package com.reservation.entity;

import com.reservation.enums.ReservationStatus;
import javax.persistence.*;
import javax.validation.constraints.*;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;

/**
 * 预定实体类
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "reservations")
public class Reservation extends BaseEntity {

    @NotNull(message = "用户ID不能为空")
    @Column(name = "user_id", nullable = false)
    private Long userId;

    @NotNull(message = "商品ID不能为空")
    @Column(name = "product_id", nullable = false)
    private Long productId;

    @NotBlank(message = "商品名称不能为空")
    @Column(name = "product_name", nullable = false, length = 100)
    private String productName;

    @NotNull(message = "商品价格不能为空")
    @Column(name = "product_price", nullable = false, precision = 12, scale = 2)
    private BigDecimal productPrice;

    @Column(name = "product_image", length = 500)
    private String productImage;

    @NotNull(message = "预定数量不能为空")
    @Min(value = 1, message = "预定数量必须大于0")
    @Column(name = "quantity", nullable = false)
    private Integer quantity;

    @NotNull(message = "总金额不能为空")
    @Column(name = "total_amount", nullable = false, precision = 12, scale = 2)
    private BigDecimal totalAmount;

    @NotNull(message = "预定状态不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    private ReservationStatus status = ReservationStatus.PENDING;

    @NotBlank(message = "联系人姓名不能为空")
    @Column(name = "contact_name", nullable = false, length = 50)
    private String contactName;

    @NotBlank(message = "联系电话不能为空")
    @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确")
    @Column(name = "contact_phone", nullable = false, length = 20)
    private String contactPhone;

    @NotBlank(message = "收货地址不能为空")
    @Column(name = "contact_address", nullable = false, length = 200)
    private String contactAddress;

    @Column(name = "notes", length = 500)
    private String notes;

    // 关联查询用的字段（不映射到数据库）
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", insertable = false, updatable = false)
    private User user;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", insertable = false, updatable = false)
    private Product product;

    /**
     * 确认预定
     */
    public void confirm() {
        if (this.status != ReservationStatus.PENDING) {
            throw new IllegalStateException("只有待确认的预定才能确认");
        }
        this.status = ReservationStatus.CONFIRMED;
    }

    /**
     * 取消预定
     */
    public void cancel() {
        if (this.status == ReservationStatus.CANCELLED || this.status == ReservationStatus.COMPLETED) {
            throw new IllegalStateException("该预定无法取消");
        }
        this.status = ReservationStatus.CANCELLED;
    }

    /**
     * 完成预定
     */
    public void complete() {
        if (this.status != ReservationStatus.CONFIRMED) {
            throw new IllegalStateException("只有已确认的预定才能完成");
        }
        this.status = ReservationStatus.COMPLETED;
    }
}
